package com.opencore.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class WindowsUtils {
	  public static boolean listRunningProcesses() {
	    List<String> processes = new ArrayList<String>();
	    boolean existe = false;
	    try {
	      String line;
	      //Process p = Runtime.getRuntime().exec("tasklist.exe /fo csv /nh");
	      Process p = Runtime.getRuntime().exec("tasklist.exe /fo csv /nh /S db_cocha3-desa /u administrator /p desa.,404");
	      BufferedReader input = new BufferedReader
	          (new InputStreamReader(p.getInputStream()));
	      while ((line = input.readLine()) != null) {
	          if (!line.trim().equals("")) {
	              // keep only the process name
	              line = line.substring(1);        	              
	              if (line.indexOf("CNTAoSMgr.exe") > -1){
	            	  existe = true;
	            	  System.out.println("SI:"+line);
	              }
	              // db_cocha3-desa administrator desa.,404
	              //TASKlist /S db_cocha3-desa /u administrator /p desa.,404   CNTAoSMgr.exe
	              // System.out.println(">>"+line);
	              //processes.add(line.substring(0, 5)); // line.indexOf("'")));
	              processes.add(line+"\n");
	          }
	      }
	      input.close();
	    }
	    catch (Exception err) {
	      err.printStackTrace();
	    }
	    return existe;
	  }

	  public static void main(String[] args){
	     boolean exiteProceso = listRunningProcesses();
	     if (exiteProceso){
	    	 System.out.println("SI EXISTE");
	     }else{
	    	 System.out.println("NO EXISTE");
	     }
	     
	  }

	 
	}
